<?php

namespace app\common\business;

use app\common\model\mysql\SBanner as SBannerModel;
use app\common\model\mysql\SRubik as SRubikModel;

class SPlatform
{
    public $bannerObj = null;
    public $rubikObj = null;
    public function __construct()
    {
        $this->bannerObj = new SBannerModel();
        $this->rubikObj = new SRubikModel();
    }

    /**
     * 轮播图查询
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalBanner($where,$field,$limit,$page){
        $dictData = $this->bannerObj->getNormalBannerList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    /**
     * 轮播图查询
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalBannerNotPage($where,$field){
        $dictData = $this->bannerObj->getNormalBannerNotPageList($where,$field);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加轮播图
    public function addBanner($data){
        try {
            $this->bannerObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->bannerObj->id;
    }

    //轮播图修改
    public function updateBannerById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            "banner_id"=>$id,
        ];
        return $this->bannerObj->where($where)->find()->save($data);
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getBannerById($id){
        $user = $this->bannerObj->getbannerIdData($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$ids数组，删除数据库记录delete
    public function delBannerIds($ids){
        $ids = explode(',', $ids);
        $result = $this->bannerObj->delBannerIds($ids);
        return $result;
    }

    /******************* 魔方管理模块 ******************************/
    /**
     * 魔方查询
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalRubik($where,$field,$limit,$page){
        $dictData = $this->rubikObj->getNormalRubikList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加魔方
    public function addRubik($data){
        try {
            $this->rubikObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->rubikObj->id;
    }

    //魔方修改
    public function updateRubikById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            "rubik_id"=>$id,
        ];
        return $this->rubikObj->where($where)->find()->save($data);
    }

    //根据$ids数组，删除数据库记录delete
    public function delRubikIds($ids){
        $ids = explode(',', $ids);
        $result = $this->rubikObj->delRubikIds($ids);
        return $result;
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getRubikById($id){
        $user = $this->rubikObj->getRubikIdData($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }
}
